package com.gzbugu.bugusys.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.RequestAware;
import org.springframework.transaction.annotation.Transactional;

import com.gzbugu.bugusys.dao.IBaseDAO;
import com.gzbugu.bugusys.vo.Attendance;
import com.gzbugu.bugusys.vo.Staff;
import com.gzbugu.bugusys.vo.Type;
import com.opensymphony.xwork2.ActionSupport;

@Transactional
public class UpdateAttendanceAction extends ActionSupport implements
		RequestAware {
	@Resource(name = "baseDAO")
	private IBaseDAO baseDAO;

	private Map<String, Object> request;
	private String attendanceTypeName;
	private Attendance attendance;
	private String staffId;
	private Integer attendanceId;
	private List<String> errorList;
	Staff staff = null;

	public String update() {
		Type attendanceType = null;
		Attendance attendancePer = (Attendance) baseDAO.findById(attendanceId,
				com.gzbugu.bugusys.fields.VoClasseFields.ATTENDANCE);
		List list = baseDAO.findByProperty("name", attendanceTypeName,
				com.gzbugu.bugusys.fields.VoClasseFields.TYPE);
		if (list != null) {
			attendanceType = (Type) list.get(0);
		}
		attendancePer.setStart(attendance.getStart());
		attendancePer.setEnd(attendance.getEnd());
		attendancePer.setPayment(attendance.getPayment());
		attendancePer.setType(attendanceType);
		attendancePer.setStaff(staff);
		baseDAO.attachDirty(attendancePer);
		return SUCCESS;
	}

	public String alter() {
		if (attendanceId != null && !("".equals(attendanceId))) {
			attendance = (Attendance) baseDAO.findById(attendanceId,
					com.gzbugu.bugusys.fields.VoClasseFields.ATTENDANCE);
			request.put("methodName", "alter");
		}
		return SUCCESS;
	}

	public String delete() {
		if (attendanceId != null && !("".equals(attendanceId))) {
			Attendance attend = (Attendance) baseDAO.findById(attendanceId,
					com.gzbugu.bugusys.fields.VoClasseFields.ATTENDANCE);
			if (attend != null) {
				baseDAO.delete(attend);
			}
		}
		return SUCCESS;
	}

	public void validateUpdate() {
		if ("".equals(this.staffId) || this.staffId == null) {
			this.addFieldError("staffIdError", "员工编号不能为空！");
		} else {
			staff = (Staff) baseDAO.findById(this.staffId,
					com.gzbugu.bugusys.fields.VoClasseFields.STAFF);
			if (staff == null) {
				this.addFieldError("staffIdError", "编号为" + staffId + "的员工不存在！");
			}
		}
		if (attendance.getStart() == null || "".equals(attendance.getStart())
				|| attendance.getEnd() == null
				|| "".equals(attendance.getEnd())) {
			this.addFieldError("timeError", "请填写考勤起止时间！");
		} else {
			if (attendance.getEnd().getTime() - attendance.getStart().getTime() <= 0) {
				this.addFieldError("timeError", "起始时间有冲突！");
			}
		}
	}

	public String getAttendanceTypeName() {
		return attendanceTypeName;
	}

	public void setAttendanceTypeName(String attendanceTypeName) {
		this.attendanceTypeName = attendanceTypeName;
	}

	public Attendance getAttendance() {
		return attendance;
	}

	public void setAttendance(Attendance attendance) {
		this.attendance = attendance;
	}

	public String getStaffId() {
		return staffId;
	}

	public void setStaffId(String staffId) {
		this.staffId = staffId;
	}

	public List<String> getErrorList() {
		return errorList;
	}

	public void setErrorList(List<String> errorList) {
		this.errorList = errorList;
	}

	public Integer getAttendanceId() {
		return attendanceId;
	}

	public void setAttendanceId(Integer attendanceId) {
		this.attendanceId = attendanceId;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

}
